package com.woniu34.order.config;

import com.woniu.common.constant.Constants;
import org.springframework.amqp.core.*;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;
import java.util.Map;

@Configuration
public class DlxMqConfig {


    @Bean(value = "delayExchange")
    public DirectExchange delayExchange(){
        return new DirectExchange(Constants.MQ_ORDER_DELAY_EXCHANGE_300S);
    }

    @Bean(value = "dlxExchange")
    public DirectExchange dlxExchange(){
        return new DirectExchange(Constants.MQ_ORDER_DLX_EXCHANGE_300S);
    }

    @Bean(value = "delayQueue")
    public Queue delayQueue(){

        Map<String,Object> args = new HashMap<>();
        args.put("x-message-ttl",60000);
        args.put("x-dead-letter-exchange",Constants.MQ_ORDER_DLX_EXCHANGE_300S);
        args.put("x-dead-letter-routing-key",Constants.MQ_ORDER_DLX_ROUTING_KEY_300S);

        Queue queue = QueueBuilder.durable(Constants.MQ_ORDER_DELAY_QUEUE_300S).withArguments(args).build();

        return queue;

    }

    @Bean(value = "dlxQueue")
    public Queue dlxQueue(){
        Queue queue = new Queue(Constants.MQ_ORDER_DLX_QUEUE_300S);
        return queue;

    }

    @Bean
    public Binding delayExchangeQueueBinding(@Qualifier("delayExchange") DirectExchange delayExchange,
                                             @Qualifier("delayQueue") Queue delayQueue){
        Binding binding = BindingBuilder.bind(delayQueue).to(delayExchange).with(Constants.MQ_ORDER_DELAY_ROUTING_KEY_300S);

        return binding;

    }

    @Bean
    public Binding dlxExchangeQueueBinding(@Qualifier("dlxExchange") DirectExchange dlxExchange,
                                             @Qualifier("dlxQueue") Queue dlxQueue){
        Binding binding = BindingBuilder.bind(dlxQueue).to(dlxExchange).with(Constants.MQ_ORDER_DLX_ROUTING_KEY_300S);

        return binding;

    }

}
